// csharp-adapter.js
import { NodeType } from '../ast.js';

// C#标识符映射表
const identifierMap = {
  '年龄': 'age',
  '信息框': 'Console.WriteLine'
};

// C#类型映射
const typeMap = {
  '整数': 'int',
  '文本': 'string'
};

export default {
  transform(ast) {
    switch (ast.type) {
      case NodeType.VariableDecl:
        const csType = typeMap[ast.dataType];
        const csValue = ast.dataType === '文本' ? `"${ast.value}"` : ast.value;
        const csName = identifierMap[ast.name] || ast.name;
        return `${csType} ${csName} = ${csValue};`;
        
      case NodeType.IfStatement:
        return `if (${this.generateCondition(ast.condition)}) {
    ${this.transform(ast.thenBranch)}
}`;
        
      case NodeType.FunctionCall:
        const csFuncName = identifierMap[ast.name] || ast.name;
        const csArgs = ast.args.map(arg => typeof arg === 'string' ? `"${arg}"` : arg);
        return `${csFuncName}(${csArgs.join(', ')});`;
        
      case NodeType.BinaryOp:
        const csLeft = identifierMap[ast.left] || ast.left;
        const csRight = typeof ast.right === 'string' ? identifierMap[ast.right] || ast.right : ast.right;
        return `${csLeft} ${ast.op} ${csRight}`;
        
      default:
        throw new Error(`不支持的AST节点类型: ${ast.type}`);
    }
  },
  
  generateCondition(condition) {
    if (condition.type === NodeType.BinaryOp) {
      const csLeft = identifierMap[condition.left] || condition.left;
      const csRight = typeof condition.right === 'string' ? identifierMap[condition.right] || condition.right : condition.right;
      return `${csLeft} ${condition.op} ${csRight}`;
    }
    throw new Error('不支持的条件表达式类型');
  },
  
  // C#代码生成器需要的头部信息
  getHeader() {
    return 'using System;\n';
  },
  
  // C#主函数模板
  getMainTemplate(codeLines) {
    return `class Program {
    static void Main() {
        ${codeLines.join('\n        ')}
    }
}`;
  }
};